package com.foreknow.dao.impl;

import com.foreknow.dao.UserDAO;
import com.foreknow.db.DBManager;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.mapping.UserMapping;
import com.foreknow.model.User;
import com.foreknow.util.BaseDAO;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl extends BaseDAO implements UserDAO {
  @Override
  public boolean addUser(User user) {
    //1.对数据库添加操作
    int isRigth = jdbcTemplate.update("insert into user(username,password) values(?,?)",user.getUsername(),user.getPassword());
    return isRigth==1;
  }

  @Override
  public List<User> getQueryAll() {
    //从工厂中获取UserMapping对象
    EntityMapping mapping =  mappingFactory.getMap(MappingFactory.USER_MAPPING);//UserMapping
    List<Object> list = jdbcTemplate.query("select * from user",mapping);
    List<User> listUser = new ArrayList<>();
   for(int i = 0;i<list.size();i++){
     User user = (User)list.get(i);
     listUser.add(user);
   }
    return listUser;
  }

  @Override
  public User getQueryById(Integer id) {
    return null;
  }

  @Override
  public boolean deleteUser(Integer id) {
    int isRigth = jdbcTemplate.update("delete from user where id=?",id);
    return isRigth==1;
  }

  @Override
  public boolean updateUser(User user) {
    return false;
  }
}
